let $image = $('#image')
let option = {
    aspectRatio: 1,
    preview: '.img-preview'
}
$image.cropper(option)
$('#file').on('change', function () {

    if (this.files.length > 0) {
        let fileObj = this.files[0]
        let url = URL.createObjectURL(fileObj)
        console.log(url)
        $image.cropper('replace', url)

    }
})
$(".sss").on('click', function () {
    $('#file').trigger('click')
})
$('#sure').on('click', function () {
    let canvas = $image.cropper('getCroppedCanvas', {
        width: 30,
        height: 30
    })
    let base64 = canvas.toDataURL()
    console.log(base64)
    $.ajax({
        type: "POST",
        url: "/my/user/avatar",
        data: {
            avatar: base64

        },
        success: function (res) {
            if (res.status === 0) {
                layer.msg(res.message)
                window.parent.getInform()

            }
        }
    })
})
/* $('#sure').on('click', function () {
    // 1. 剪裁图片，得到canvas
    var canvas = $image.cropper('getCroppedCanvas', {
        width: 30,
        height: 30
    });
    // 2. 把canvas转成base64格式字符串
    var base64 = canvas.toDataURL();
    // console.log(base64)
    // 3. ajax提交即可
    $.ajax({
        type: 'POST',
        url: '/my/user/avatar',
        data: {
            avatar: base64
        }, // { id: 1 }
        success: function (res) {

            if (res.status === 0) {
                layer.msg(res.message);
                // 更新index.html的头像
                window.parent.getInform();
            }
        }
    })
}) */